//
//  ToastContentView.swift
//  TCToast
//
//  Created by 叮叮 on 2021/3/17.
//

import Foundation
import UIKit

class ToastLoadingView: UIView {
    
    /// 视图内容
    var contentView: UIView?
    
    /// 加载指示器
    var indicator: UIActivityIndicatorView?
    
    /// 显示文本
    var textLabel: UILabel?
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setContentView()
        setContentViewConstraint()
        setIndicatorLabelContentView()
        
    }
    
    /// 设置内容视图
    func setContentView() {
        contentView = UIView()
        contentView?.translatesAutoresizingMaskIntoConstraints = false
        contentView?.layer.cornerRadius = 15
        contentView?.layer.masksToBounds = true
        contentView?.layer.backgroundColor = UIColor.white.withAlphaComponent(0.5).cgColor
        addSubview(contentView!)
        
        var style = UIBlurEffect.Style.light
        if (TCToast.toastDisplayType == .dark) {
            style = UIBlurEffect.Style.dark
        }
        let blur = UIBlurEffect(style: style)
        let visual = UIVisualEffectView(effect: blur)
        visual.translatesAutoresizingMaskIntoConstraints = false
        contentView?.addSubview(visual)
        
        matchParentView(superView: contentView, subView: visual)
    }
    
    /// 设置内容视图约束
    func setContentViewConstraint() {
        matchParentView(superView: self, subView: contentView!)
    }
    
    /// 子视图等于父视图
    func matchParentView(superView: UIView?, subView: UIView) {
        
        guard let view = superView else { return }
        
        let topConstraint = NSLayoutConstraint(item: subView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: subView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
        let leftConstraint = NSLayoutConstraint(item: subView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: subView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)

        view.addConstraint(topConstraint)
        view.addConstraint(bottomConstraint)
        view.addConstraint(leftConstraint)
        view.addConstraint(rightConstraint)
    }
    
    /// 设置加载指示器和文本内容视图
    func setIndicatorLabelContentView() {
        let mixedView = UIView()
        mixedView.translatesAutoresizingMaskIntoConstraints = false
        contentView?.addSubview(mixedView)
        
        let indicator = setIndicator()
        mixedView.addSubview(indicator)
        setIndicatorConstraint(superView: mixedView)
        
        let textLabel = setLabel()
        mixedView.addSubview(textLabel)
        setLabelConstraint(superView: mixedView)
        
        guard let content = contentView else { return }
        
        let leftConstraint = NSLayoutConstraint(item: mixedView, attribute: .left, relatedBy: .greaterThanOrEqual, toItem: content, attribute: .left, multiplier: 1, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: mixedView, attribute: .right, relatedBy: .lessThanOrEqual, toItem: content, attribute: .right, multiplier: 1, constant: 0)
        let centerYConstraint = NSLayoutConstraint(item: mixedView, attribute: .centerY, relatedBy: .equal, toItem: content, attribute: .centerY, multiplier: 1, constant: 0)
        let centerXConstraint = NSLayoutConstraint(item: mixedView, attribute: .centerX, relatedBy: .equal, toItem: content, attribute: .centerX, multiplier: 1, constant: 0)
        let topConstraint = NSLayoutConstraint(item: mixedView, attribute: .top, relatedBy: .lessThanOrEqual, toItem: content, attribute: .top, multiplier: 1, constant: 10)
        let bottomConstraint = NSLayoutConstraint(item: mixedView, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: content, attribute: .bottom, multiplier: 1, constant: -10)
        let heighgtConstraint = NSLayoutConstraint(item: mixedView, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .height, multiplier: 1, constant: 80)

        content.addConstraint(leftConstraint)
        content.addConstraint(rightConstraint)
        content.addConstraint(centerYConstraint)
        content.addConstraint(centerXConstraint)
        content.addConstraint(topConstraint)
        content.addConstraint(bottomConstraint)
        content.addConstraint(heighgtConstraint)
    }
    
    /// 设置载入图标
    func setIndicator() -> UIActivityIndicatorView {
        indicator = UIActivityIndicatorView(style: .medium)
        if TCToast.toastDisplayType == .light {
            indicator?.color = .black
        } else {
            indicator?.color = .white
        }
        indicator?.translatesAutoresizingMaskIntoConstraints = false
        indicator?.startAnimating()
        return indicator!
    }
    
    /// 设置载入图标约束
    func setIndicatorConstraint(superView: UIView) {
        
        guard let indicator = indicator else { return }
        
        let leftConstraint = NSLayoutConstraint(item: indicator, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1, constant: 10)
        let cneterYConstraint = NSLayoutConstraint(item: indicator, attribute: .centerY, relatedBy: .equal, toItem: superView, attribute: .centerY, multiplier: 1, constant: 0)

        superView.addConstraint(leftConstraint)
        superView.addConstraint(cneterYConstraint)
    }
    
    /// 设置显示文本
    func setLabel() -> UILabel {
        textLabel = UILabel()
        textLabel?.numberOfLines = 0
        textLabel?.translatesAutoresizingMaskIntoConstraints = false
        if TCToast.toastDisplayType == .light {
            textLabel?.textColor = .black
        } else {
            textLabel?.textColor = .white
        }
        return textLabel!
    }
    
    /// 设置显示文本约束
    func setLabelConstraint(superView: UIView) {
        
        guard let textLabel = textLabel else { return }
        
        let leftConstraint = NSLayoutConstraint(item: textLabel, attribute: .left, relatedBy: .equal, toItem: indicator!, attribute: .right, multiplier: 1, constant: 10)
        let rightConstraint = NSLayoutConstraint(item: textLabel, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1, constant: -10)
        let topConstraint = NSLayoutConstraint(item: textLabel, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1, constant: 10)
        let bottomConstraint = NSLayoutConstraint(item: textLabel, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1, constant: -10)

        superView.addConstraint(leftConstraint)
        superView.addConstraint(rightConstraint)
        superView.addConstraint(topConstraint)
        superView.addConstraint(bottomConstraint)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
